package com.example.springhelloworld.services;

import com.example.springhelloworld.mapper.SoDao;
import com.example.springhelloworld.model.VO.SoVo;
import com.example.springhelloworld.pojo.So;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ClusterOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

@Service
public class SoServiceImpl implements SoService{

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    public static final String CACHE_USER_INFO_PREFIX="userinfo";

    @Autowired
    private SoDao soDao;


    @Override
    public List<SoVo> queryByItemsStatus(int status) {
        String key = CACHE_USER_INFO_PREFIX +status;
        ValueOperations operations = redisTemplate.opsForValue();
        Object object = operations.get(key);
        if (null!=object){
            System.out.println("使用了缓存");
            return (List<SoVo>) object;
        }

        List<SoVo> soVos = soDao.queryByItemsStatus(status);
        operations.set(key,soVos);
        return soVos;
    }

    @Override
    public List<So>  queryByItems() {
        return soDao.queryByItems();
    }
}
